//
//  TopBar.m
//  Dolphin
//
//  Created by Handymood on 11-6-7.
//  Copyright 2011 __Hanydmood__. All rights reserved.
//

#import "TopBar.h"
#import "IPhone_IPad_Judge.h"
@implementation TopBar
@synthesize cur_link;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
        newTopBg = [[UIImageLayer alloc] initWithFrame:CGRectMake(0, 0, 768, 43)];        
        [newTopBg initImage:@"xian_top_1.png"
            useOriginalSize:TRUE
                  ratioMode:1
                  layerInfo:Nil];
        [self addSubview:newTopBg];
        
        //init the up toolbar
        upsideToolBar = [[UIToolbar alloc] initWithFrame:frame];
        upsideToolBar.barStyle = UIBarStyleBlackOpaque;// UIBarStyleBlackTranslucent;
        upsideToolBar.alpha = 0.6;
        //[self addSubview: upsideToolBar];
        
        indexTextfield = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 130, frame.size.height)];
        indexTextfield.textAlignment = UITextAlignmentCenter;
        indexTextfield.enabled = false;
        indexTextfield.borderStyle = UITextBorderStyleNone;//UITextBorderStyleRoundedRect;
        indexTextfield.textColor = [UIColor whiteColor]; //text color
        indexTextfield.font = [UIFont systemFontOfSize:frame.size.height*0.7];  //font size
        indexTextfield.frame = CGRectMake(indexTextfield.frame.origin.x, 0.10*frame.size.height, 
                                                                   indexTextfield.frame.size.width,indexTextfield.frame.size.height);
        indexTextfield.backgroundColor =[UIColor clearColor];
        indexTextfield.autocorrectionType = UITextAutocorrectionTypeNo;	// no auto correction support    
        //[self addSubview:indexTextfield];
        
        categoryButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        categoryButton.button_id = 1000;
        
        if ([IPhone_IPad_Judge isIPad]) 
        {
            [categoryButton  addButtonWithImageLink:@"catlog_btn.png" downStatusImgLink:@"catlog_btn.png"];
        }
        else
        {
            [categoryButton  addButtonWithImageLink:@"catlog_btn.png" downStatusImgLink:@"catlog_btn.png"];
        }
        
        //categoryButton.hidden = TRUE;
        [categoryButton  setXY:0 offy:0];
        [categoryButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:categoryButton];  
        /*
        shareButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        shareButton.button_id = 2000;
        if ([IPhone_IPad_Judge isIPad]) 
        {
            [shareButton  addButtonWithImageLink:@"catlog_btn.png" downStatusImgLink:@"catlog_btn.png"];
        }
        else
        {
            [shareButton addButtonWithImageLink:@"catlog_btn.png" downStatusImgLink:@"catlog_btn.png"];
        }
        [shareButton setXY:0 offy:0];
        [shareButton addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        */
        //[self addSubview:shareButton];
        

        
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void) addAudio:(NSString *)audioPath autoPlayAtInit:(BOOL) autoPlayInit
{
    /*
    bgSoundPlayer = [[SimpleAudioPlayer alloc] init];
    [bgSoundPlayer initAudioPlayer:audioPath autoPlay:autoPlayInit isLoop:YES isButtonShow:YES];
    [bgSoundPlayer setXY:0 offy:0]; 
     */
    //[self addSubview:bgSoundPlayer]; 
}

-(void) addSlideshowPlayButton:(BOOL) autoPlay;
{
    slidePlayToogleButton = [[UIToogleButton alloc] init];
    [slidePlayToogleButton buttonInit:@"slideshow_play_btn.png" inPauseBtnPath:@"slideshow_pause_btn.png" autoPlay:autoPlay];
    //[self addSubview:slidePlayToogleButton];
    
    indexTextfield.hidden = TRUE;
}

-(void) initWithToolBarInfo:(UpToolBarInfo *)upToolBarInfo
{
    //add home link
    if(upToolBarInfo.home_link != Nil && [upToolBarInfo.home_link length]>0)
    {
        home_link = [[NSString alloc] initWithString:upToolBarInfo.home_link];
        
        homeButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        homeButton.button_id = 0;
        [homeButton  addButtonWithImageLink:@"head_btn.png" downStatusImgLink:@"head_btn.png"];
        [homeButton  setXY:0 offy:0];
        [homeButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [self addSubview:homeButton];  
    }
    
    //add facebook link
    if(upToolBarInfo.facebook_link != Nil && [upToolBarInfo.facebook_link length] > 0)
    {
        facebook_link = [[NSString alloc] initWithString:upToolBarInfo.facebook_link];
        
        facebookButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        facebookButton.button_id = 1;
        [facebookButton  addButtonWithImageLink:@"facebook_logo.png" downStatusImgLink:@"facebook_logo.png"];
        [facebookButton  setXY:55 offy:0];
        [facebookButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        //[self addSubview:facebookButton];  
    }
    
    //add twitter link
    if(upToolBarInfo.twitter_link != Nil && [upToolBarInfo.twitter_link length] > 0)
    {
        twitter_link= [[NSString alloc] initWithString:upToolBarInfo.twitter_link];

        twitterButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
        twitterButton.button_id = 2;
        [twitterButton  addButtonWithImageLink:@"twitter_logo.png" downStatusImgLink:@"twitter_logo.png"];
        [twitterButton  setXY:110 offy:0];
        [twitterButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        //[self addSubview:twitterButton];  
    }
    
    if (!upToolBarInfo.page_index_display) 
    {
        indexTextfield.hidden = TRUE;
    }
    
    openType = upToolBarInfo.openType;
}

-(void) mouseDownProc:(id)sender
{
    ImageUIButton *button = sender;

    if (button != Nil) 
    {
        if(button.button_id == 0 && home_link != Nil)
        {
            cur_link = home_link;
            
            if (openType == 1) 
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cur_link]];
            }
            else
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"open_link" object:self];
            }
            
        }
        else if(button.button_id == 1 && facebook_link != Nil)
        {
            cur_link = facebook_link;
            
            if (openType == 1) 
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cur_link]];
            }
            else
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"open_link" object:self];
            }
        }
        else if(button.button_id == 2 && twitter_link != Nil)
        {
            cur_link = twitter_link;
            
            if (openType == 1) 
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:cur_link]];
            }
            else
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"open_link" object:self];
            }
        }
        else if(button.button_id == 1000)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"show_category" object:self];
        }
        else if(button.button_id == 2000)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"share_button_trigger" object:self];
        }
    }
}

-(void) setAutoPlayStatus:(BOOL) autoPlay
{
    if (autoPlay) 
    {
        [slidePlayToogleButton mouseDownProcPlay:Nil];
    }
    else
    {
        [slidePlayToogleButton mouseDownProcPause:Nil];
    }
}

-(void) updateIndex:(int) curIndex _totalIndex:(int) totalIndex
{
    indexTextfield.text = [NSString stringWithFormat:@"%d/%d",curIndex,totalIndex];
}

-(void) layoutAdjustWithOrientation:(UIInterfaceOrientation)interfaceOrientation
                                    isFullScreen:(BOOL)fullscreen;
{
    CGRect screen_rect = [[UIScreen mainScreen] bounds];

    if (!fullscreen) 
    {
        screen_rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    }
    
    int snd_btn_width = 0;
    int up_tool_bar_width = 0;
    int extend_width = 10;
    
    if ([IPhone_IPad_Judge isIPad])  
    {
        extend_width = 15;
    }
    
    if(interfaceOrientation == UIInterfaceOrientationPortrait || 
       interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        upsideToolBar.frame = CGRectMake(0, 0, screen_rect.size.width, upsideToolBar.frame.size.height);
        
        if (bgSoundPlayer != Nil) 
        {
            CGFloat scale = (upsideToolBar.frame.size.height*0.65)/bgSoundPlayer.frame.size.height;

            [bgSoundPlayer buttonSizeAdjust:bgSoundPlayer.frame.size.width*scale height:bgSoundPlayer.frame.size.height*scale];
            [bgSoundPlayer setXY:screen_rect.size.width-bgSoundPlayer.frame.size.width-5 
                                          offy:(upsideToolBar.frame.size.height-bgSoundPlayer.frame.size.height)/2]; 
            
            snd_btn_width = bgSoundPlayer.frame.size.width+extend_width; 
        }
        
        if (slidePlayToogleButton != Nil) 
        {
            CGFloat scale = (upsideToolBar.frame.size.height*0.65)/slidePlayToogleButton.frame.size.height;
            
            [slidePlayToogleButton buttonSizeAdjust:slidePlayToogleButton.frame.size.width*scale height:slidePlayToogleButton.frame.size.height*scale];            
            [slidePlayToogleButton setXY:screen_rect.size.width/2 - slidePlayToogleButton.frame.size.width/2 
                                                     offy:(upsideToolBar.frame.size.height-slidePlayToogleButton.frame.size.height)/2]; 
        }
        
        up_tool_bar_width = screen_rect.size.width;
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        upsideToolBar.frame = CGRectMake(0, 0, screen_rect.size.height, upsideToolBar.frame.size.height);
        
        if (bgSoundPlayer != Nil) 
        {    
            CGFloat scale = (upsideToolBar.frame.size.height*0.65)/bgSoundPlayer.frame.size.height;

            [bgSoundPlayer buttonSizeAdjust:bgSoundPlayer.frame.size.width*scale height:bgSoundPlayer.frame.size.height*scale];
            [bgSoundPlayer setXY:screen_rect.size.height-bgSoundPlayer.frame.size.width-5 
                                          offy: (upsideToolBar.frame.size.height-bgSoundPlayer.frame.size.height)/2]; 
            
            snd_btn_width = bgSoundPlayer.frame.size.width+extend_width; 
        }
    
        if (slidePlayToogleButton != Nil) 
        {
            CGFloat scale = (upsideToolBar.frame.size.height*0.65)/slidePlayToogleButton.frame.size.height;
            
            [slidePlayToogleButton buttonSizeAdjust:slidePlayToogleButton.frame.size.width*scale height:slidePlayToogleButton.frame.size.height*scale];
            [slidePlayToogleButton setXY:screen_rect.size.height/2 - slidePlayToogleButton.frame.size.width/2
                                                     offy:(upsideToolBar.frame.size.height - slidePlayToogleButton.frame.size.height)/2]; 
        }    
        
        up_tool_bar_width = screen_rect.size.height;
    }
    
    [self buttonSizeAdjust:homeButton];
    //[self buttonSizeAdjust:facebookButton];
    //[self buttonSizeAdjust:twitterButton];

    categoryButton.frame = CGRectMake(up_tool_bar_width-snd_btn_width-categoryButton.frame.size.width-5,
                                                                 (upsideToolBar.frame.size.height-categoryButton.frame.size.height)/2-2,categoryButton.frame.size.width,categoryButton.frame.size.height);
    if([IPhone_IPad_Judge isIPad])
    {
        indexTextfield.frame = CGRectMake(upsideToolBar.frame.size.width/2-indexTextfield.frame.size.width/2,
                                          indexTextfield.frame.origin.y,
                                          indexTextfield.frame.size.width, indexTextfield.frame.size.height);
    }
    else
    {
        indexTextfield.frame = CGRectMake(upsideToolBar.frame.size.width/2-indexTextfield.frame.size.width/2+10,
                                          indexTextfield.frame.origin.y,
                                          indexTextfield.frame.size.width, indexTextfield.frame.size.height);
    }
    

    [homeButton setXY:homeButton.frame.size.width*0.25 offy:homeButton.frame.origin.y];
    [facebookButton setXY:homeButton.frame.origin.x+1.45*homeButton.frame.size.width offy:facebookButton.frame.origin.y];
    [twitterButton setXY:homeButton.frame.origin.x+2.9*homeButton.frame.size.width offy:twitterButton.frame.origin.y];
    
    /*
    shareButton.frame = CGRectMake(homeButton.frame.origin.x+4.35*homeButton.frame.size.width,
                                   (upsideToolBar.frame.size.height-shareButton.frame.size.height)/2+1,
                                   shareButton.frame.size.width,
                                   shareButton.frame.size.height);
    */
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, upsideToolBar.frame.size.width, upsideToolBar.frame.size.height);
}

-(void) buttonSizeAdjust:(ImageUIButton *)imageUIButton
{
    if (imageUIButton != Nil)
    {
        CGFloat scale = (upsideToolBar.frame.size.height*0.65)/imageUIButton.frame.size.height;
        
        imageUIButton.frame = CGRectMake(imageUIButton.frame.origin.x,upsideToolBar.frame.size.height*0.175,
                                         imageUIButton.frame.size.width*scale,imageUIButton.frame.size.height*scale);
    }
}

-(void) reset
{
    [upsideToolBar removeFromSuperview];
    [upsideToolBar release];
    
    if (bgSoundPlayer != Nil) 
    {
        [bgSoundPlayer reset];
        [bgSoundPlayer removeFromSuperview];
        [bgSoundPlayer release];
    }
    
    if (slidePlayToogleButton != Nil) 
    {
        [slidePlayToogleButton reset];
        [slidePlayToogleButton removeFromSuperview];
        [slidePlayToogleButton release];
    }
    
    if (indexTextfield != Nil) 
    {
        [indexTextfield removeFromSuperview];
        [indexTextfield release];
    }
    
    if(homeButton != Nil)
    {
        [homeButton removeTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [homeButton setUserInteractionEnabled:NO];
        [homeButton removeFromSuperview];
    }

    if(facebookButton != Nil)
    {
        [facebookButton removeTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [facebookButton setUserInteractionEnabled:NO];
        [facebookButton removeFromSuperview];
    }

    if (twitterButton != Nil) 
    {
        [twitterButton removeTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
        [twitterButton setUserInteractionEnabled:NO];
        [twitterButton removeFromSuperview];
    }
}

-(BOOL) musicOn
{
    if (bgSoundPlayer != Nil) 
    {
        return [bgSoundPlayer musicOn];
    }
    else
    {
        return FALSE;
    }
}

-(BOOL) slideOnAutoPlay
{
    return [slidePlayToogleButton onPlay];
}

-(void) manuallyMusicOn
{
    if (bgSoundPlayer != Nil)
    {
        [bgSoundPlayer manuallyMusicOn];
    }
}

-(void) manuallyMusicOff
{
    if (bgSoundPlayer != Nil)
    {
        [bgSoundPlayer manuallyMusicOff];
    }
}

-(void) manuallyPlaySlide
{
    if (slidePlayToogleButton != Nil) 
    {
        [slidePlayToogleButton manuallyPlay];
    }  
}

-(void) manuallyPauseSlide
{
    if (slidePlayToogleButton != Nil) 
    {
        [slidePlayToogleButton manuallyPause];
    }
}

-(void) displayAlbumBtn
{
    if (categoryButton) 
    {
        categoryButton.hidden = FALSE;
    }
    
}

- (void)dealloc
{
    [super dealloc];
}

@end
